﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using Wk.System.Domain.Services;
using System.Domain.Entities;
using fabao.Common.Entity;

namespace tianzedianzi.Controllers.System
{
    public class MenuController : BaseController
    {
        private readonly ILogger<MenuController> _logger;
        private readonly MenuService _menuService;
        private readonly IMapper _mapper;
        //private readonly IRestService _rest;
        public MenuController(ILogger<MenuController> logger, IMapper mapper, MenuService menuService)
        {
            _logger = logger;
            _menuService = menuService;
            _mapper = mapper;
        }
        public IActionResult MenuIndex()
        {
            return View("~/Views/System/Menu/MenuIndex.cshtml");
        }
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetMenuList()
        {
            var menulist = await _menuService.GetAllMenusAsync();
            return Success("",menulist);
        }

        /// <summary>
        /// 添加菜单页面
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> AddMenu(int menuId = 0, int parentId = 0)
        {
            Menu menu = new Menu();
            menu.parentId = parentId;
            if (menuId > 0)
            {
                menu =await _menuService.GetOneMenuAsync(menuId);
            }
            return View("~/Views/System/Menu/AddMenu.cshtml", menu);
        }
        /// <summary>
        /// 添加按钮页面
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> AddBtn(int menuId = 0, int parentId = 0)
        {
            Menu menu = new Menu();
            menu.parentId = parentId;
            if (menuId > 0)
            {
                menu = await _menuService.GetOneMenuAsync(menuId);
            }
            return View("~/Views/System/Menu/AddBtn.cshtml", menu);
        }
        /// <summary>
        /// 添加菜单请求
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add(Menu menu)
        {
            bool isSuccess = false;
            if (menu.id > 0)
            {
                isSuccess = await _menuService.UpdateMenuAsync(menu);
            }
            else
            {
                isSuccess = await _menuService.AddMenuAsync(menu);
            }

            if (isSuccess)
                return Success();
            else
                return Error("保存失败");
        }
       
        /// <summary>
        /// 图表库选择页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Icon()
        {
            return View("~/Views/System/Menu/Icon.cshtml");
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete(int menuId = 0)
        {
            var result = await _menuService.DeleteMenuAsync(menuId);
            if (result)
                return Success();
            else
                return Error();
        }
    }
}
